<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /**
       * 递归
       *    函数自己调用自己
       *
       *    栈：先进（调用）后出（完成）
       *
       * 【
       *    fn(10)
       *        console.log(10, "start");
       *        fn(9)
       *            console.log(9, "start");
       *            fn(8)
       *                console.log(8, "start");
       *                fn(7)
       *                    console.log(7, "start");
       *                    ...
       *                    fn(1)
       *                        console.log(1, "start");
       *                        fn(0)
       *                            console.log(0, "start");
       *                            console.log(0, "end");
       *                        console.log(1, "end");
       *                    console.log(2, "end");
       *                console.log(7, "end");
       *            console.log(8, "end");
       *        console.log(9, "end");
       *    console.log(10, "end");
       * 】
       *
       * 10,98,7,6,5,4,3,2,1,0
       * */

      //   function fn(v, r) {
      //     // console.log(v, "start");
      //     r += v;
      //     console.log(r);
      //     if (v > 0) {
      //       fn(v - 1, r);
      //     }
      //     // console.log(v, "end");
      //   }

      //   fn(10, 0);

      // 1+...+100  = > (1+100)*50 高斯
      function f(count) {
        if (count <= 2) {
          return 1;
        }
        return f(count - 1) + f(count - 2);
      }

      const result = f(10);
      // f(9) + f(8)
      // f(8) + f(7) + f(8)
      // f(7) + f(6) + f(7) + f(8)
      // f(6) + f(5)  + f(6) + f(7) + f(8)
      // f(5) + f(4) + f(5)  + f(6) + f(7) + f(8)
      // f(4) + f(3) + f(4) + f(5)  + f(6) + f(7) + f(8)
      // f(3) + f(2) + f(3) + f(4) + f(5)  + f(6) + f(7) + f(8)
      // f(2) + f(1) + f(2) + f(3) + f(4) + f(5)  + f(6) + f(7) + f(8)
      //1 + f(1) + f(2) + f(3) + f(4) + f(5)  + f(6) + f(7) + f(8)
      //1 + 1 + f(2) + f(3) + f(4) + f(5)  + f(6) + f(7) + f(8)
      //1 + 1 + 1 + f(3) + f(4) + f(5)  + f(6) + f(7) + f(8)
      //1 + 1 + 1 + f(2) + f(1) + f(4) + f(5)  + f(6) + f(7) + f(8)
      //1 + 1 + 1 + 1 + 1 + f(4) + f(5)  + f(6) + f(7) + f(8)
      console.log("result", result);
    </script>
  </body>
</html>
